package com.sbcb.properties;

import com.sbcb.properties.config.PropertiesListener;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

//@ServletComponentScan //会扫描Filter
@SpringBootApplication
public class PropertiesApplication {
    public static void main(String[] args) {
//        SpringApplication.run(PropertiesApplication.class, args);

        //使用监听器加载配置文件
        SpringApplication app = new SpringApplication(PropertiesApplication.class);
        //注意，默认查找的是classes目录下的文件
        app.addListeners(new PropertiesListener("config/propertiesListenerLoad.properties"));
        app.run(args);
    }

    //如何同时加载yml和properties配置文件
    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer = new
                PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("config/complicatedProperties.yml"));
        configurer.setProperties(yaml.getObject());
        return configurer;
    }
}
